using System;
using AntEater.ComponentModel.Validation;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Drawing.Design;
using System.Diagnostics.CodeAnalysis;

namespace AntEater.ComponentModel.BuildElements
{
    /// <summary>
    /// Provides the OnError build element.
    /// </summary>
    [Serializable]
    [BuildElement("OnError")]
    [Icon(typeof(OnErrorBuildElement), "AntEater.ComponentModel.Resources.OnError.ico")]
    public class OnErrorBuildElement : BuildElementBase
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="T:OnErrorBuildElement"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public OnErrorBuildElement(BuildFileDefinition context)
            : base(context) {
            _executeTargets = new TargetCollection(context);
            _executeTargets.CollectionChanged += new EventHandler<CollectionChangeEventArgs>(ExecuteTargetsCollectionChanged);
        }

        private string _condition;

        /// <summary>
        /// Gets or sets the condition.
        /// </summary>
        /// <value>The condition.</value>
        [BuildElementProperty("Condition", false, BuildElementPropertyType.Condition)]
        [Description("Optional attribute.\nCondition to be evaluated.")]
        [Category("MsBuild")]
        public string Condition {
            get { return _condition; }
            set {
                if (_condition != value) {
                    _condition = value;
                    OnPropertyChanged("Condition");
                }
            }
        }

        private TargetCollection _executeTargets;

        /// <summary>
        /// Gets or sets the execute targets.
        /// </summary>
        /// <value>The execute targets.</value>
        [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        [BuildElementProperty("ExecuteTargets", true, BuildElementPropertyType.ListOfTargets)]
        [Description("Required attribute.\nThe targets to execute if a task fails. Separate multiple targets with semicolons. Multiple targets are executed in the order specified.")]
        [Category("MsBuild")]
        [Editor(typeof(Design.TargetCollectionTypeEditor), typeof(UITypeEditor))]
        public TargetCollection ExecuteTargets {
            get { return _executeTargets; }
            set { _executeTargets = value; }
        }

        /// <summary>
        /// Determines whether this instance is valid.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// 	<c>true</c> if this instance is valid; otherwise, <c>false</c>.
        /// </returns>
        public override bool IsValid {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Validates the specified recursive.
        /// </summary>
        /// <param name="recursive">if set to <c>true</c> [recursive].</param>
        /// <returns></returns>
        public override ReadOnlyCollection<ValidationResult> Validate(bool recursive) {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        public override object Clone() {
            OnErrorBuildElement newElement = base.Clone() as OnErrorBuildElement;

            if (this._condition != null) {
                newElement._condition = this._condition.Clone() as string;
            }
            newElement._executeTargets.AddRange(this._executeTargets.Clone() as TargetCollection);

            return newElement;
        }

        /// <summary>
        /// Executes the targets collection changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:System.ComponentModel.CollectionChangeEventArgs"/> instance containing the event data.</param>
        private void ExecuteTargetsCollectionChanged(object sender, CollectionChangeEventArgs e) {
            OnPropertyChanged("ExecuteTargets");
        }
    }
}
